package ims.com.shlcm.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Customized setting support
 * @author lld
 * modified by Dennis
 */
public class CmSettings {
   private static Map<String, Map<String, String>> settings        = new HashMap<String, Map<String, String>>();
   private Map<String, String>                     sectionSettings = null;

   static {
      try {
         loadSettings();
      } catch (Exception e) {
         throw new ExceptionInInitializerError(e);
      }
   }

   private CmSettings(Map<String, String> m) {
      if (m == null)
         m = new HashMap<String, String>();
      sectionSettings = m;
   }

   public CmSettings getSection(Map<String, String> sectionSettings) {
      if (sectionSettings == null)
         sectionSettings = new HashMap<String, String>();
      return new CmSettings(sectionSettings);
   }

   public static CmSettings getSection(String section) {
      Map<String, String> result = settings.get(section);
      if (result == null)
         result = new HashMap<String, String>();
      return new CmSettings(result);
   }

   @SuppressWarnings("rawtypes")
   public Set keySet() {
      return sectionSettings.keySet();
   }

   public String get(String key) {
      return (String) sectionSettings.get(key);
   }

   public String get(String key, String defaultVal) {
      String val = (String) sectionSettings.get(key);
      return val == null ? defaultVal : val;
   }

   public int get(String key, int defaultVal) {
      String val = (String) sectionSettings.get(key);
      if (val == null)
         return defaultVal;

      return Integer.parseInt(val);
   }

   @SuppressWarnings("rawtypes")
   public Map getSectionMap() {
      return sectionSettings == null ? new HashMap() : sectionSettings;
   }

   @SuppressWarnings("rawtypes")
   private static final Set TRUES = new HashSet<String>(Arrays.asList(new String[]{"TRUE", "YES", "ON", "1"}));

   public boolean get(String key, boolean defaultVal) {
      String val = (String) sectionSettings.get(key);
      if (val == null)
         return defaultVal;

      return TRUES.contains(val.toUpperCase()) ? true : false;
   }

   private static void loadSettings() {
      try {
         settings.clear();
         InputStream fis = CmSettings.class.getResourceAsStream("/ims/com/shlcm/conf/ims.ini");
         BufferedReader r = new BufferedReader(new InputStreamReader(fis, "GBK"));

         String line;
         String section = null;
         Map<String, String> sectMap = null;
         while ((line = r.readLine()) != null) {
            if (line.startsWith("#") || line.startsWith(";"))
               continue;

            if (line.startsWith("[") && line.indexOf("]") > 0) {
               section = line;
               sectMap = new HashMap<String, String>();
               settings.put(section, sectMap);
               settings.put(section.substring(1, line.length() - 1).trim(), sectMap);
               continue;
            }

            if (section == null)
               continue;
            int deliPos = line.indexOf("=");
            if (deliPos < 0)
               continue;

            String key = line.substring(0, deliPos).trim();
            String val = line.substring(deliPos + 1, line.length()).trim();
            if (key.startsWith("\"") && key.endsWith("\""))
               key = key.substring(1, key.length() - 1);
            if (val.startsWith("\"") && val.endsWith("\""))
               val = val.substring(1, val.length() - 1);
            sectMap.put(key, val);
         }
      } catch (Exception e) {
         System.out.println(">>>Error in Settings.loadSettings()" + e.toString());
         e.printStackTrace();
      }

   }
}
